const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const SAFEZONE_GRID_DATA = require("../test/210216_grids_in_safezone-yulin.json");
let GRID_WIDTH = SAFEZONE_GRID_DATA.GRID_LENGTH;
// debugger
var median = function (array) {
  array = array.sort((a,b)=>a-b);
  if (array.length % 2 === 0) { // array with even number elements
    return (array[array.length / 2] + array[(array.length / 2) - 1]) / 2;
  }
  else {
    return array[(array.length - 1) / 2]; // array with odd number elements
  }
};
mongoConnected.then(async $ => {
  let zones = (await mongoModels.TB21_city.find({}));
  let safevalmax = lodash.max(zones.map(e => e.dev.safezoneval));
  let zhandoumax = lodash.max(zones.map(e => e.dev.zhandouliedu));
  for (let zone of zones) {
    let safev = zone.dev.safezoneval / safevalmax;
    let zhandouv = zone.dev.zhandouliedu / zhandoumax;
    zone.dev.safetyindex = safev - 1 * zhandouv;
    // console.log(`[${zone.dev.zhandouliedu/zhandoumax},${zone.dev.safezoneval/safevalmax}],`)
    // console.log(`[${zone.dev.attacks},${zone.dev.safezoneval}],`)
  }
  let siMedian = median(zones.map(zone => zone.dev.safetyindex))
  debugger
  // let safevalMean = lodash.mean(zones.map(e => e.dev.safezoneval))
  // let zhandouMean = lodash.mean(zones.map(e => e.dev.zhandouliedu))
  // let safevalMedian = median(zones.map(e => e.dev.safezoneval));
  // let zhandouMedian = median(zones.map(e => e.dev.zhandouliedu));
  let wuziMedian = median(zones.map(e => e.dev.item_density));
  // let wuziMean = lodash.mean(zones.map(e => e.dev.item_density))
  debugger
  for (let zone of zones) {
    let is_rich = zone.dev.item_density >= wuziMedian;

    // debugger
    let is_safe = zone.dev.safetyindex>=siMedian;
    console.log(zone.name,zone.dev.safetyindex,siMedian,zone.dev.safetyindex>=siMedian)
    // let is_safe = zone.dev.safezoneval > safevalMean && zone.dev.zhandouliedu < zhandouMean;
    let city_type = is_rich ? (is_safe ? 'a' : 'b') : (is_safe ? 'c' : 'd');
    await zone.update({

      $set: {
        is_rich,
        is_safe,
        city_type
      }
    })
  }
  debugger
  return
  for (let zone of zones) {
    let mianji = (zone.extent.x[1] - zone.extent.x[0]) / 100 * ((zone.extent.y[1] - zone.extent.y[0]) / 100)
    let item_density = (zone.dev.peijian + zone.dev.wuqi + zone.dev.yaopin) / mianji;
    let zhandouliedu = zone.dev.attacks / mianji;
    await zone.update({
      $set: {
        "dev.item_density": item_density,
        "dev.zhandouliedu": zhandouliedu
      }
    })

  }
  debugger


  return
  for (let zone of zones) {
    let extent = zone.extent;
    let gridxi = extent.x.map(v => Math.floor(v / GRID_WIDTH))
    let gridyi = extent.y.map(v => Math.floor(v / GRID_WIDTH))
    let gridcount = (gridxi[1] - gridxi[0] + 1) * (gridyi[1] - gridyi[0] + 1)
    let safeval = 0;
    for (let xi = gridxi[0]; xi <= gridxi[1]; xi++) {
      for (let yi = gridyi[0]; yi <= gridyi[1]; yi++) {
        safeval += SAFEZONE_GRID_DATA.GRIDS_IN_SMALL[xi][yi]
      }
    }
    safeval = safeval / gridcount;
    // debugger
    await zone.update({
      $set: {
        "dev.safezoneval": safeval
      }
    });
  }
  debugger









  return;
  zones = (await mongoModels.TB21_city.find({}));
  zones.forEach(e => {
    e.dev.attacks = 0;
  })
  // debugger
  let match_ids = (await mongoModels.Match.aggregate([{
    $match: {
      map_name: "Savage_Main",//Desert_Main,DihorOtok_Main Savage_Main Baltic_Main
      is_customGame: false,
      is_eventMode: false,
      team_size: {
        $in: [1, 2, 4]
      }
    }
  }, {
    $project: {
      "match_id": "$match_id",
      "_id": 0
    }
  }])).map(e => e.match_id);
  for (let match_id of match_ids) {
    let evKills = (await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogPlayerKill"
    })).map(e => e._doc)
    // .filter(e => e.victim.zone.length);
    let evGroggy = (await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogPlayerMakeGroggy"
    })).map(e => e._doc)
    // .filter(e => e.victim.zone.length);
    for (let zone of zones) {
      let zonename = zone.name_id;
      let kills = evKills.filter(e => e.victim.zone.includes(zonename));
      let groggy = evGroggy.filter(e => e.victim.zone.includes(zonename));
      let count = lodash.uniq(kills.concat(groggy).map(e => e.attackId)).length;
      zone.dev.attacks += count;
      // debugger
      // let x =  await zone.update({ $set: { "dev.attacks": zone.dev.attacks } })
      // debugger
    }

    // let attackIdsKill = evKills.map(e => e.attackId);
    // let attackIdsGroggy = evGroggy.map(e => e.attackId);
    // let attackIds = lodash.uniq(attackIdsGroggy.concat(attackIdsKill))
    // debugger
    console.log(match_id, "done");
  }
  for (let zone of zones) {
    await zone.update({ $set: { "dev.attacks": zone.dev.attacks } })
  }
  debugger
})